package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.domain.CourseManagementDTO;
import com.woniu.domain.MapLearn;
import com.woniu.domain.MapPath;
import com.woniu.domain.User;
import com.woniu.service.MapLearnService;
import com.woniu.service.MapPathService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: ljy  <br/>
 * Date: 2021/6/8:16:47  <br/>
 * Description:学习地图控制器
 */
@RestController
@RequestMapping("/mapLearn")
public class MapLearnController {
    @Autowired(required = false)
    MapLearnService mapLearnService;


    @Autowired(required = false)
    MapPathService mapPathService;

    @Autowired(required = false)
    RedisTemplate redisTemplate;

    @ApiOperation(value = "新增地图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mapLearn", value = "地图对象", required = true, paramType = "body", dataType = "MapLearn", dataTypeClass = MapLearn.class),
            @ApiImplicitParam(name = "httpServletRequest", value = "用户获取请求头信息（登录token）"),
    })
    @PostMapping("/addMapLearn")
    public int addMapLearn(@RequestBody MapLearn mapLearn, HttpServletRequest httpServletRequest) {
        //从redis中通过token键值取出对应的对象
        User user = (User) redisTemplate.opsForValue().get(httpServletRequest.getHeader("Authorization"));
        mapLearn.setCreator(user.getUserName());
        MapLearn mapLearnByName = mapLearnService.findMapLearnByName(mapLearn.getMapName());
        if(mapLearnByName!=null){
            return 0;
        }else{
            return mapLearnService.addMapLearn(mapLearn);
        }

    }

    @ApiOperation(value = "通过id查询地图回显地图数据")
    @ApiImplicitParam(name = "mapId", value = "地图id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    @PostMapping("/findMapLearnById")
    public MapLearn findMapLearnById(Integer mapId) {
        System.out.println(mapId);
        return mapLearnService.findMapLearnById(mapId);
    }

    @ApiOperation(value = "通过id查询地图回显路径数据")
    @ApiImplicitParam(name = "id", value = "地图对象", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    @PostMapping("/findMapPathByIds")
    public List<MapPath> findMapPathByIds(String refPathIds){
        String[] ids = refPathIds.split(",");
        return mapPathService.findMapPathByIds(ids);
    }

    @ApiOperation(value = "修改地图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mapLearn", value = "地图对象", required = true, paramType = "body", dataType = "MapLearn", dataTypeClass = MapLearn.class),
            @ApiImplicitParam(name = "httpServletRequest", value = "用户获取请求头信息（登录token）"),
    })
    @PostMapping("/updateMapLearn")
    public int updateMapLearn(@RequestBody MapLearn mapLearn, HttpServletRequest httpServletRequest) {
        System.out.println(mapLearn);
            return mapLearnService.updateMapLearn(mapLearn);


    }



    @RequestMapping("mapList")
    public PageInfo<MapLearn> getQueryAllMap(Integer page , Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<MapLearn> queryAllMap = mapLearnService.getQueryAllMap();
        PageInfo<MapLearn> pageInfo = new PageInfo<>(queryAllMap);
        return pageInfo;
    }

    @RequestMapping("cmapList")
    public PageInfo<MapLearn> conditionQueryAllMap(@RequestBody CourseManagementDTO<MapLearn> courseManagementDTO) {
        System.out.println(courseManagementDTO.getPage()+"-"+courseManagementDTO.getPageSize());
        PageHelper.startPage(courseManagementDTO.getPage(),courseManagementDTO.getPageSize());
        List<MapLearn> queryAllMap = mapLearnService.conditionQueryAllMap((MapLearn)courseManagementDTO.getObj());
        PageInfo<MapLearn> pageInfo = new PageInfo<>(queryAllMap);
        return pageInfo;
    }

    /*
     * Description : 批量删除  <br/>
     * ChangeLog : 1. 创建 (2021/6/8 19:23 [ljy]);
     ** @param ids 要处理的id集合
     * *  @return int 返回受影响行数
    */
    @RequestMapping("delMapLearn")
    public int batchDeletion(@RequestBody List<Integer> ids){
        System.out.println(ids);
        return mapLearnService.batchDeletion(ids);
    }

    /*
     * Description : 根据id查询地图信息  <br/>
     * ChangeLog : 1. 创建 (2021/6/9 09:52 [ljy]);
     ** @param id   地图id
     * *  @return MapLearn 地图对象
    */
    @RequestMapping("getMapLearnById")
    public MapLearn getMapLearnById(Integer id){
        return mapLearnService.getMapLearnById(id);
    }

}
